#!/usr/bin/env python

import gdata.calendar.service
import wsgiref.handlers

import cgi
import os

from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
import gdata.service
import gdata.docs.service
import gdata.urlfetch
import logging

gdata.service.http_request_handler = gdata.urlfetch

class CalendarPage(webapp.RequestHandler):
    def get(self):
        parameters = cgi.FieldStorage()
        authsub_token = parameters.getvalue('token')
        logging.debug("Google authentication token: " + str(authsub_token))
        
        logging.debug(authsub_token)
        #gd_client = gdata.docs.service.DocsService("rolfst@jobiroxa.com", "01_Gandalf")
        gd_client = gdata.docs.service.DocsService()
        gd_client.auth_token = authsub_token
        gd_client.UpgradeToSessionToken()
        #gd_client = gdata.docs.service.DocsService()
        #gd_client.email = 'rolfst@jobiroxa.com'
        #gd_client.password = '01_Gandalf'
        #gd_client.ProgrammaticLogin()
        
        feed = gd_client.GetDocumentListFeed()
        values = {"items": feed}
        
        path = os.path.join(os.path.dirname(__file__), "Calendar.html")
        self.response.out.write(template.render(path, values, True))
        
def main():
  application = webapp.WSGIApplication([('/Calendar', CalendarPage)],
                                       debug=True)
  wsgiref.handlers.CGIHandler().run(application)

if __name__ == "__main__":
  main()